Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

30-Day LeetCoding Challenge 2020 April Week 4 || Leetcode 解題

30-Day LeetCoding Challenge 2020 April Week 4 || Leetcode 解題

測到一個 bug

測到一個 bug

【JS幼幼班】Step.01 學習,從「不要害怕」開始

【JS幼幼班】Step.01 學習,從「不要害怕」開始






留言討論